<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table border :data="goodslist" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">{{ scope.row.add_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editDialogBox(scope.row.goods_id)">编辑</el-button>
            <el-button type="success" icon="el-icon-delete" @click="removeDialogBox(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>

    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="34%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  data() {
    return {
      // 商品列表数组
      goodslist: [],
      // 携带请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      editDialogVisible: false,
      // 编辑商品的表单数据
      editForm: {},
      editFormRules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取全部的商品列表数据请求
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表资源失败')
      this.goodslist = res.data.goods
      this.total = res.data.total
      console.log(res)
    },
    // 监听分页显示多少条
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getGoodsList()
    },
    // 监听分页总条数
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getGoodsList()
    },
    // 点击编辑按钮 弹出对话框
    async editDialogBox(row) {
      const { data: res } = await this.$http.get(`goods/${row}`)
      if (res.meta.status !== 200) {
        return this.$message.error('查询id的参数失败')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    // 编辑商品 请求
    editGoods() {
      this.$refs.editFormRules.validate(async valid => {
        if (!valid) {
          return this.$message.error('请先填选项值')
        }
        const { data: res } = await this.$http.put(`goods/${this.editForm.goods_id}`, this.editForm)
        if (res.meta.status !== 200) return this.$message.error('修改商品失败')
        this.getGoodsList()
        this.editDialogVisible = false
        this.$message.success('修改商品成功')
      })
    },
    // 删除该当前的id商品值
    async removeDialogBox(rowID) {
      const confimOK = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confimOK !== 'confirm') {
        return this.$message.info('已取消删除')
      } else {
        const { data: res } = await this.$http.delete(`goods/${rowID}`)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.getGoodsList()
        this.$message.success('删除商品列表成功')
      }
    },
    // 点击添加按钮 跳转到add页面
    addGoods() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped></style>
